Git Commands: A Complete Guide for Developers

Git Commands: A Complete Guide for Developers

Git is a distributed version control system used by developers to track changes in their code, collaborate with teams, and manage different versions of a project efficiently. Whether you are a beginner or an experienced developer, knowing essential Git commands is crucial for improving workflow and productivity.

In this guide, we will cover all essential Git commands, their usage, practical examples, and common mistakes to avoid.


What is Git?

Git is a version control system that allows multiple developers to work on the same project without conflicts. It enables:

✅ Tracking changes in the codebase
✅ Collaborating with team members
✅ Maintaining different versions of the project
✅ Rolling back changes if needed

Let’s explore Git commands that every developer should know!


 Basic Git Commands

Basic Git commands help you initialize repositories, track changes, and commit updates.

CommandDescriptionExampleExplanation
git initInitializes a new Git repositorygit init my-projectCreates a .git folder to start version control.
git clone <repository_url>Clones an existing repositorygit clone https://github.com/user/repo.gitCopies a remote repository to your local machine.
git add <file>Adds a file to the staging areagit add index.htmlPrepares a file for commit.
git commit -m "message"Saves changes to the repositorygit commit -m "Initial commit"Creates a snapshot of changes with a message.
git statusShows the current status of the working directorygit statusDisplays modified and untracked files.
git logDisplays commit historygit log --onelineShows a list of commits with their IDs.
git mv <old_name> <new_name>Renames or moves a filegit mv old.txt new.txtHelps rename a file while maintaining history.

 Branching and Merging


Branches allow developers to work on new features or bug fixes without affecting the main project.

CommandDescriptionExampleExplanation
git branchLists, creates, or deletes branchesgit branch feature-branchCreates a new branch for a feature.
git checkout <branch>Switches between branchesgit checkout feature-branchMoves to a different branch.
git switch <branch>Alternative to git checkoutgit switch mainA modern command to switch branches.
git merge <branch>Merges changes from one branch to anothergit merge feature-branchCombines changes from feature-branch into the current branch.
git rebase <branch>Reapplies changes from one branch onto anothergit rebase mainKeeps commit history clean while merging.

🚀 Example Workflow:

git checkout -b new-feature   # Create a new branch
git add .                     # Stage changes
git commit -m "Added a new feature"  # Commit changes
git checkout main             # Switch to main branch
git merge new-feature         # Merge changes into main

 Commit and History

CommandDescriptionExample
git diffShows changes between commits and working directorygit diff
git logDisplays commit historygit log --graph --oneline --all
git showDisplays details of a specific commitgit show HEAD
git blame <file>Shows who last modified each line of a filegit blame index.html
git commit --amendModifies the last commitgit commit --amend -m "Updated message"

 Working with Remote Repositories

CommandDescriptionExample
git remoteManages remote repositoriesgit remote add origin https://github.com/user/repo.git
git fetchDownloads objects from a remote repositorygit fetch origin
git pullFetches and merges changes from remotegit pull origin main
git pushUploads local commits to a remote repositorygit push origin main

Undo & Fix Mistakes

CommandDescriptionExample
git restore <file>Discards changes in working directorygit restore index.html
git reset --soft HEAD~1Undo last commit but keep changes stagedgit reset --soft HEAD~1
git reset --mixed HEAD~1Undo last commit and unstage changesgit reset --mixed HEAD~1
git reset --hard HEAD~1Undo last commit and delete changesgit reset --hard HEAD~1

 Git Hooks (Automation & Scripting)

CommandDescription
pre-commitHook that runs before commit
post-commitHook that runs after commit
pre-pushHook that runs before push

Git Submodules

CommandDescriptionExample
git submodule add <repo_url>Adds a submodule to a repogit submodule add https://github.com/user/lib.git
git submodule update --init --recursiveUpdates submodules recursivelygit submodule update --init --recursive
git submodule foreach git pullUpdates all submodulesgit submodule foreach git pull origin main

Git  Advanced Command

1. Advanced Branching & Merging

  • git merge --no-ff <branch> → Merges a branch while preserving its history.
  • git rebase --onto <new-base> <old-base> <branch> → Moves a branch onto a new base.

2. Undoing Changes

  • git restore <file> → Discards changes in the working directory.
  • git restore --staged <file> → Unstages a file without removing changes.
  • git reset --soft HEAD~1 → Removes the last commit but keeps changes staged.
  • git reflog → Shows a log of all HEAD movements (useful for recovering lost commits).

3. Debugging & Fixing Issues

  • git bisect → Finds a faulty commit by binary search.
  • git fsck → Checks the integrity of Git objects and repository.

4. Working with Remote Repositories

  • git remote -v → Displays remote repositories with their URLs.
  • git remote rm <name> → Removes a remote repository reference.

5. Working with Submodules

  • git submodule add <repo_url> → Adds a submodule (useful for linking repositories).
  • git submodule update --init --recursive → Initializes and updates submodules.

6. Configuration & Optimization

  • git config --global user.name "Your Name" → Sets global user name.
  • git config --global user.email "your.email@example.com" → Sets global user email.
  • git gc → Cleans up unnecessary files to optimize repository performance.

7. Miscellaneous

  • git shortlog -sn → Displays commit statistics by author.
  • git archive --format=zip HEAD > latest.zip → Exports the latest version of the repository as a ZIP file.

Common Git Mistakes & Fixes

1️⃣ Committed to the Wrong Branch?

git checkout correct-branch
git cherry-pick <commit-hash>
git reset --hard HEAD~1

2️⃣ Pushed Wrong Changes to Remote?

git reset --hard HEAD~1
git push --force origin main

3️⃣ Merge Conflicts?

git status  
git mergetool  
git commit -m "Resolved conflicts"


We are Recommending you:

Leave a comment

Comments